package com.frcdi.qms.web.controller.masterdata;

import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.masterdata.domain.Project;
import com.frcdi.qms.masterdata.service.IProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 检验项目Controller
 *
 * @author cell
 * @date 2023-09-12
 */
@Api(tags = "检验项目")
@RestController
@RequestMapping("/masterData/project")
public class ProjectController extends BaseController
{
    @Autowired
    private IProjectService projectService;

    /**
     * 查询检验项目列表
     */
    @ApiOperation("查询检验项目列表")
    @PreAuthorize("@ss.hasPermi('masterData:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project)
    {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 导出检验项目列表
     */
    @ApiOperation("导出检验项目列表")
    @PreAuthorize("@ss.hasPermi('masterData:project:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Project project)
    {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        util.exportExcel(response, list, "检验项目数据");
    }

    /**
     * 获取检验项目详细信息
     */
    @ApiOperation("获取检验项目详细信息")
    @PreAuthorize("@ss.hasPermi('masterData:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(projectService.selectProjectById(id));
    }

    /**
     * 新增检验项目
     */
    @ApiOperation("新增检验项目")
    @PreAuthorize("@ss.hasPermi('masterData:project:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Project project)
    {
        if (!projectService.checkprojectNameUnique(project))
        {
            return error("新增检验项目'" + project.getProjectName() + "'失败，名称已存在");
        }
        project.setCreateBy(getUsername());
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改检验项目
     */
    @ApiOperation("修改检验项目")
    @PreAuthorize("@ss.hasPermi('masterData:project:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Project project)
    {
        if (!projectService.checkprojectNameUnique(project))
        {
            return error("修改检验项目'" + project.getProjectName() + "'失败，名称已存在");
        }
        project.setUpdateBy(getUsername());
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除检验项目
     */
    @ApiOperation("删除检验项目")
    @PreAuthorize("@ss.hasPermi('masterData:project:remove')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids)
    {
        return toAjax(projectService.deleteProjectByIds(ids));
    }
}
